<?
/**
*  报表数据处理
*  2014/5/13 by zh
*/

include("database.php");

include("session.php");
require("ajax_base.php");

$action = $_GET['action'];


if($action == "select_key_report") {
	$dic_arr = array();
	$select_key_desc = $db->getOneValue("select select_key_desc from qc_task where id=".$_GET['task_id']);
	if($select_key_desc != '') {
		$desc_arr = split(",", $select_key_desc);
		$cnt = count($desc_arr);
		for($i=0; $i<$cnt; $i++) {
			$kv = split(":", $desc_arr[$i]);
			$dic_arr[$kv[0]] = $kv[1];
		}
	}
	$sql = "select count(0) as cnt, user_select_key from qc_call where task_id=".$_GET['task_id']." and call_result=1 group by user_select_key";
	$db->query($sql);
	$total = 0;
	$data = array();
	while($db->nextRecord()) {
		$key = $db->f("user_select_key");
		$label = "按(".$key.")键";
		if($key == "") {
			$label = "未按键";
		} else if(array_key_exists($key, $dic_arr)) {
			$label = $dic_arr[$key];
		}
		$data[] = "{'label':'".$label."','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	print_json("【呼叫成功总数($total)个】", "A", "B", $data);
}

else if($action == "failed_call_report") {
	$sql = "select count(0) as cnt, fail_desc from qc_call where task_id=".$_GET['task_id']." and call_result=2 group by fail_desc";
	$db->query($sql);
	$total = 0;
	$data = array();
	while($db->nextRecord()) {
		$label = $dic_data['fail_desc'][$db->f("fail_desc")];
		$data[] = "{'label':'".$label."','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	print_json("【呼叫失败总数($total)个】", "A", "B", $data);
}

else if($action == "day_call_report") {
	$start = date("Y-m-d", strtotime("-7 days"));
	$end = date("Y-m-d");
	$sql = "select count(0) as cnt, DAY(call_time), DATE(call_time) as ymd from qc_call a Left Join qc_task b On a.task_id=b.id where 1=1 
	and call_status=2 and call_time between '$start 00:00:00' and '$end 23:59:59' group by DAY(call_time)";
	if( empty($_GET['task_id']) ) {
		if($_SESSION['user_flag'] != "admin") {
			$sql = str_replace("1=1", "b.user_id=".$_SESSION['user_id'], $sql);
		} else if( ! empty($_GET['user_id']) ) {
			$sql = str_replace("1=1", "b.user_id=".$_GET['user_id'], $sql);
		}
	} else {
		$sql = str_replace("1=1", "a.task_id=".$_GET['task_id'], $sql);
	}
	$db->query($sql);
	$total = 0;
	$data = array();
	while($db->nextRecord()) {
		$data[] = "{'label':'".$db->f("ymd")."','value':'".$db->f("cnt")."', 'link':'j-show_time_report_by_date-".$db->f("ymd")."'}";
		$total += $db->f("cnt");
	}
	print_json("【最近7天外呼总计($total)个】", "日期", "外呼量", $data);
}

else if($action == "time_call_report") {
	$dt = $_GET['dt'];
	$sql = "select count(0) as cnt, HOUR(call_time) as h from qc_call a Left Join qc_task b On a.task_id=b.id where 1=1 
	and call_status=2 and call_time between '$dt 00:00:00' and '$dt 23:59:59' group by  HOUR(call_time)";
	if( empty($_GET['task_id']) ) {
		if($_SESSION['user_flag'] != "admin") {
			$sql = str_replace("1=1", "b.user_id=".$_SESSION['user_id'], $sql);
		} else if( ! empty($_GET['user_id']) ) {
			$sql = str_replace("1=1", "b.user_id=".$_GET['user_id'], $sql);
		}
	} else {
		$sql = str_replace("1=1", "a.task_id=".$_GET['task_id'], $sql);
	}
	$db->query($sql);
	$total = 0;
	$data = array();
	while($db->nextRecord()) {
		$data[] = "{'label':'".$db->f("h")."','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	print_json("【{$dt}总计($total)个】", "时间", "外呼量", $data);
}


/////////////////////////////////////   下面的分支暂时没有用到 ////////////////////////////////////////////
else if($action == "task_report") {
	$sql = "select sum(success_count) as success_sum, sum(fail_count) as fail_sum from qc_task where 1=1";
	$sql = add_condition($sql);
	$db->query($sql);
	$db->nextRecord();
	$data[] = "{'label':'成功条数','value':'".$db->f("success_sum")."'}";
	$data[] = "{'label':'失败条数','value':'".$db->f("fail_sum")."'}";
	$total = $db->f("success_sum") + $db->f("fail_sum");
	print_json("【所有呼叫总数($total)个】", "A", "B", $data);
}
else if($action == "call_report") {
	$sql = "select count(a.id) as fail_count, fail_desc from qc_call a Left Join qc_task b On a.task_id=b.id where 1=1 and call_result=2 group by fail_desc";
	$sql = add_condition($sql);
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$dic_data['fail_desc'][$db->f("fail_desc")]."','value':'".$db->f("fail_count")."'}";
		$total += $db->f("fail_count");
	}
	print_json("【失败记录总数($total)个】", "A", "B", $data);	
}
else if($action == "task_top5_report") {
	$sql = "select task_name, call_count, success_count, fail_count from qc_task where 1=1 order by call_count desc limit 0,10";
	$sql = add_condition($sql);
	$db->query($sql);
	$cate = array();
	$data = array('call_count'=>array(), 'success_count'=>array(), 'fail_count'=>array());
	while($db->moveNext()) {
		$cate[] = $db->f("task_name");
		$data['call_count'][] = $db->f("call_count");
		$data['success_count'][] = $db->f("success_count");
		$data['fail_count'][] = $db->f("fail_count");
	}
	print_m3d_xml($cate, $data);	
}
else if($action == "call_top5_report") {
	$sql = "SELECT COUNT(a.id) as cnt, DATE_FORMAT(call_time,'%e') AS call_day FROM qc_call a Left Join qc_task b On a.task_id=b.id where 1=1 GROUP BY call_day limit 0,5";
	$sql = add_condition($sql);
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$db->f("call_day")."日','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	print_json("【近5天外呼总数($total)个】", "近5天日期", "外呼条数", $data);	
}


function add_condition($src_sql) {
	$condition = "1=1";
	if($_SESSION['user_flag'] != "admin") {
		$condition .= " and user_id=".$_SESSION['user_id'];
	}
	if(isset($_GET['date_range'])) {
		//$arr = explode("_", $_GET['date_range']);
		//$condition .= " and order_date < '".$_GET['date_end']."'"; 
	}
	$_SESSION['report_date_range'] = $_GET['date_range']; // 保存一下时间段，以备链接报表输出
	$src_sql = str_replace("1=1", $condition, $src_sql);
	return $src_sql;
}


function print_json($title, $xname, $yname, $data) {
	echo "{";
	echo "'chart': {";
	echo "'caption' : '$title',";
	echo "'xAxisName' : '$xname',";
	echo "'yAxisName' : '$yname',";
	echo "'formatNumberScale' : '0',";
	if($xname == "CHANNEL")  {
		// for Bar2D Only
		//---------------------------------
		echo "'alternateVGridColor' : 'AFD8F8',";
		echo "'toolTipBorderColor' : '114B78',";
		echo "'baseFontColor' : '114B78',";
		echo "'toolTipBgColor' : 'E7EFF6',";
		echo "'plotBorderDashed' : '1',";
		echo "'plotBorderDashLen' : '2',";
		echo "'useRoundEdges' : '1',";   
		echo "'plotBorderDashGap' : '2',"; 
		echo "'showBorder' : '0',";   
		echo "'bgColor' : 'FFFFFF,FFFFFF',";   
		//---------------------------------
	}
	echo "'baseFontSize' : '12'},";        
	echo "'data' : [";               
	echo join(", ", $data);
	echo "]";
	echo "}";
}


function print_m3d_xml($cate, $data) {
	$xml = "<chart caption=\"呼叫总数任务前5名\" showlabels=\"1\" showvalues=\"0\" decimals=\"0\" numberprefix=\"\" baseFontSize=\"12\" palettecolors=\"#AFD8F8,#F6BD0F,#8BBA00\" >";
	$xml .= "<categories>";
	foreach($cate as $k=>$v) {
		$xml .= "<category label=\"".$v."\"/>";
	}
	$xml .= "</categories>";
	$title = array('call_count'=>"呼叫总数", 'success_count'=>"成功条数", 'fail_count'=>"失败条数");
	foreach($data as $k=>$v) {
		$xml .= "<dataset seriesname=\"".$title[$k]."\">";
		foreach($v as $k=>$c) {
			$xml .= "<set value=\"$c\" />";
		}
		$xml .= "</dataset>";
	}
	$xml .="</chart>";
	echo $xml;
}



